package com.h2.ref.server.web.user.account;

import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.h2.common.util.email.IEmailProvider;
import com.h2.ref.server.auth.token.IRandomTokenService;
import com.h2.ref.server.user.IUserService;
import com.h2.ref.server.user.model.User;
import com.h2.ref.server.web.ViewConstants;

@Controller
public class RecoverUserCtrlr {
   
   private IUserService _userService;
   
   private IRandomTokenService _tokenService;
   
   private IEmailProvider _emailProvider;
   
   private MessageSourceAccessor _messages =
      SpringSecurityMessageSource.getAccessor();
   
   private String _baseUrl;
   
   private UserActivationController _userActivationController;
   
   ////
   ////
   
   @ModelAttribute("user")
   public User populateUser() {
      User vo = new User();
      return vo;
   }
   
   @RequestMapping(value="/recover/user.html", method=RequestMethod.GET)
   public String setupForm() {
      return "user.recover";
   }
   
   @RequestMapping(value="/recover/user.html", method=RequestMethod.POST)
   public String processSubmit(@ModelAttribute("user") User user,
         BindingResult result) {
      
      User userDetails = null;
      if (!StringUtils.hasLength(user.getUserDetails().getEmail())) {
         //userName
         result.rejectValue("email", "field.required");
      }
      else {
         userDetails = getUserService().findByEmail(user.getUserDetails().getEmail());
         if(userDetails == null ||
               !StringUtils.hasLength(userDetails.getUserDetails().getEmail())) {
            result.rejectValue("email", "recover.email.invalid");
         }
      }
      
      if (result.hasErrors()) {
         return "user.recover";
      }
      
      if(!userDetails.isActive()) {
         getUserActivationController().sendActivationEmail(userDetails);
         return ViewConstants.REDIRECT
               + ViewConstants.ACCOUNT_EMAIL_SEND_ACTIVATE + ".html";
      }
      
      return null;
   }

   /**
    * @return the userService
    */
   public IUserService getUserService() {
      return _userService;
   }

   /**
    * @param userService the userService to set
    */
   public void setUserService(IUserService userService) {
      _userService = userService;
   }

   /**
    * @return the emailProvider
    */
   public IEmailProvider getEmailProvider() {
      return _emailProvider;
   }

   /**
    * @param emailProvider the emailProvider to set
    */
   public void setEmailProvider(IEmailProvider emailProvider) {
      _emailProvider = emailProvider;
   }

   /**
    * @return the tokenService
    */
   public IRandomTokenService getTokenService() {
      return _tokenService;
   }

   /**
    * @param tokenService the tokenService to set
    */
   public void setTokenService(IRandomTokenService tokenService) {
      _tokenService = tokenService;
   }

   /**
    * @return the messages
    */
   public MessageSourceAccessor getMessages() {
      return _messages;
   }

   /**
    * @param messages the messages to set
    */
   public void setMessages(MessageSourceAccessor messages) {
      _messages = messages;
   }

   /**
    * @return the baseUrl
    */
   public String getBaseUrl() {
      return _baseUrl;
   }

   /**
    * @param baseUrl the baseUrl to set
    */
   public void setBaseUrl(String baseUrl) {
      _baseUrl = baseUrl;
   }

   /**
    * @return the userActivationController
    */
   public UserActivationController getUserActivationController() {
      return _userActivationController;
   }

   /**
    * @param userActivationController the userActivationController to set
    */
   public void setUserActivationController(
         UserActivationController userActivationController) {
      _userActivationController = userActivationController;
   }
}
